package com.yxe.controller;


import com.yxe.api.CommonResult;
import com.yxe.pojo.Repairs;
import com.yxe.service.RepairsService;
import com.yxe.utils.AliyunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzq
 * @since 2022-12-09
 */
@RestController
@RequestMapping("/repairs")
@Slf4j
@Api(tags = "报修信息")
public class RepairsController {

    @Resource
    private RepairsService repairsService;

    @Autowired
    private AliyunOssUtil ossUtil;

    @PostMapping("addRepairs")
    @ApiOperation("上传报修信息")
    @ResponseBody
    public CommonResult addRepairs(@RequestParam("repairsAddress") String repairsAddress,
                                   @RequestParam("repairsCause") String repairsCause,
                                   @RequestParam("repairsPicture") MultipartFile repairsPicture,
                                   @RequestParam("repairsVideo") MultipartFile repairsVideo,
                                   @RequestParam("userId") Integer userId) {
        // 将文件添加到集合中
        ArrayList<MultipartFile> fileList = new ArrayList<>();
        fileList.add(repairsPicture);
        fileList.add(repairsVideo);

        List<String> stringList = new ArrayList<>();

        try {
            for (MultipartFile file : fileList) {
                if (file != null){
                    String filename = file.getOriginalFilename();

                    if (!filename.trim().equals("")){
                        File newFile = new File(filename);
                        FileOutputStream os = new FileOutputStream(newFile);
                        os.write(file.getBytes());
                        os.close();

                        //把file里的内容复制到newFile中
                        file.transferTo(newFile);
                        String upload = ossUtil.upload(newFile);

                        // 拼接产生完整地址
                        String newUpload = "https://baiyang425.oss-cn-hangzhou.aliyuncs.com/".concat(upload);


                        //图片回显地址:
                        System.out.println("图片地址" + newUpload);

                        stringList.add(upload);
                    }

                }
            }

            Repairs repairs = new Repairs();
            repairs.setRepairsAddress(repairsAddress);
            repairs.setRepairsCause(repairsCause);
            repairs.setUserId(userId);
            repairs.setRepairsPicture(stringList.get(0));
            repairs.setRepairsVideo(stringList.get(1));

            // 添加报修信息到数据库中
            repairsService.addRepairs(repairs);


            return CommonResult.success("上传成功");


        } catch (IOException e) {
            e.printStackTrace();
        }

        return CommonResult.failed("上传失败");
    }

    @PostMapping("deleteRepairs")
    @ApiOperation("删除报修信息")
    @ResponseBody
    public CommonResult deleteRepairs(String id) {

        repairsService.deleteRepairs(id);

        return CommonResult.success("删除成功");
    }


}

